﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Chefbook.DataAccess;

namespace Chefbook.ViewModels
{
    /// <summary>
    /// A view-model for a list of recipes
    /// </summary>
    public class RecipeListViewModel : ViewModelBase
    {
        private CategoryViewModel category;
        private ObservableCollection<RecipeViewModel> recipes = new ObservableCollection<RecipeViewModel>();
        private ChefbookXmlRepository repository;

        /// <summary>
        /// View Model for the list of recipes in a category
        /// </summary>
        /// <param name="category">The category whose recipes are being shown</param>
        /// <param name="repo">The data repository</param>
        public RecipeListViewModel(CategoryViewModel category, ChefbookXmlRepository repo)
        {
            CurrentCategory = category;
            repository = repo;
        }

        /// <summary>
        /// The current category whose recipes are being displayed
        /// </summary>
        public CategoryViewModel CurrentCategory
        {
            get { return category; }
            set
            {
                if (category != value)
                {
                    recipes.Clear();
                    category = value;
                    if (category != null)
                    {
                        ((INotifyCollectionChanged)category.AllRecipes).CollectionChanged += AllRecipesCollectionChanged;
                        foreach (var item in category.AllRecipes)
                        {
                            recipes.Add(item);
                        }
                    }
                    SelectedRecipe = recipes.FirstOrDefault();
                    OnPropertyChanged("CurrentCategory");
                }
            }
        }

        private RecipeViewModel selectedRecipe; 
        public RecipeViewModel SelectedRecipe
        {
            get { return selectedRecipe; }
            set
            {
                if (selectedRecipe == value)
                { return; }
                selectedRecipe = value;
                OnPropertyChanged("SelectedRecipe");
            }
        }

        /// <summary>
        /// List of recipes to display
        /// </summary>
        public ReadOnlyObservableCollection<RecipeViewModel> Recipes
        {
            get { return new ReadOnlyObservableCollection<RecipeViewModel>(recipes); }
        }

        void AllRecipesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    recipes.Add((RecipeViewModel)e.NewItems[0]);
                    if (recipes.Count == 1)
                    { SelectedRecipe = recipes.FirstOrDefault(); }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    var recipeToRemove = (RecipeViewModel)e.OldItems[0];
                    int removedIndex = recipes.IndexOf(recipeToRemove);
                    recipes.Remove((RecipeViewModel)e.OldItems[0]);
                    if (recipes.Count == 0)
                    { SelectedRecipe = null; }
                    else if (SelectedRecipe.Equals(recipeToRemove))
                    {   //If we removed the selected recipe, then the new selection should be the one after it
                        //which occupies the same index, but if the removed one was the last one in the list
                        //then select the one prior to it
                        SelectedRecipe = removedIndex == recipes.Count ? 
                            recipes[removedIndex - 1] : recipes[removedIndex];
                    }
                    break;
                default:
                    throw new NotImplementedException();
            }
        }
    }
}
